--[[
	rounds.lua
	A rounds system develped as part of the hide and seek gamemode by blankthemuffin and Auro.
--]]


local MINPLAYERS = HNSConfig["min_players"]

-- The main function where all the round magic happens.
function RoundThink()

	HNS_TIMELEFT = HNS_TIMELEFT - 1
	
	-- If we don't have enough players move into waiting and tell the clients. If we are not already waiting that is.
	if ( MINPLAYERS >= #player.GetAll() ) && HNS_GAMESTATUS != GAMESTATUS_WAITING then
	
		SetGameState( GAMESTATUS_WAITING )
		return false -- Don't need to do anything else here.
		
	end
	
	
	-- If we are in the waiting mode we do not want to do any of the following, allow the player to do what ever the hell they want basically.
	if HNS_GAMESTATUS == GAMESTATUS_WAITING then
	
		-- If we have < 4?? players then stay in waiting. Otherwise move onto starting
		if ( #player.GetAll() > MINPLAYERS ) && HNS_GAMESTATUS != GAMESTATUS_STARTING then -- Way to put the sign the wrong way round Auro. :D We also need to check that were not alread in starting lol.
		
			-- We are moving into starting 
			SetGameState( GAMESTATUS_STARTING )
			return false
			
		end
		
	end
	
	
	if HNS_TIMELEFT == 0 then
	
		if HNS_GAMESTATUS == GAMESTATUS_RUNNING then
		
			-- The round is over, or players have just joined. We should be showing a box with who won. Not to mention reseting the timers and starting a new round.	
			-- Put us in the starting mode
			SetGameState( GAMESTATUS_STARTING )
			return false
			
		elseif HNS_GAMESTATUS == GAMESTATUS_STARTING then
		
			-- In this case we actually want to start the game off and prevent new players joining the fray of this round.
			SetGameState( GAMESTATUS_RUNNING )
			return false
			
		end
		
	end
	
	if team.NumPlayers( TEAM_HIDE ) == 1 && HNS_GAMESTATUS == GAMESTATUS_RUNNING then
	
		SetGameState( GAMESTATUS_STARTING )
	
	end
	
end
timer.Create( "RoundThinkTimer", 1, 0, RoundThink )

function SetGameState( newState )
	if newState == GAMESTATUS_WAITING then
		-- We need to let the players do whatever the hell they like, as well as a bar telling them that they are waiting for players.
		HNS_TIMELEFT = -1
		
	elseif newState == GAMESTATUS_STARTING then
		-- We need to pop up a team selection dialogue, stop all spawning.
		for k, ply in pairs( player.GetAll() ) do
		
			ply:Lock()
			
			local hiders = team.GetPlayers( TEAM_HIDE )
			if #hiders == 1 then
				ply:PrintMessage( HUD_PRINTCENTER, hiders[1]:Nick() .. " Has won the round!!" )
			end
			
			ply:SendLua( "GAMEMODE.ShowScoreboard = true" ) -- TODO: Fix this.
		
		end
		
		-- Give a timelimit of 30s for players to select a team, if no team selected, shove them into spectators for that round.
		-- Also, set the new state.
		HNS_TIMELEFT = 10
	
	elseif newState == GAMESTATUS_RUNNING then
		-- Set everyone to their prefered team ( if possible ), if no team selected put them in spectators
		-- Spawn and gogogo.

		HNS_TIMELEFT = HNSConfig["round_time"]
		-- Reset round stats.
		
	end

	HNS_GAMESTATUS = newState
	
	SyncGameStatus()
	SyncTimeLeft()
	
	-- Another if after everything is synced. so we can kill everyone and not have them go straight to the damn spectators team.
	if newState == GAMESTATUS_RUNNING then
		
		local players = player.GetAll()
		
		local luckyply = players[ math.random( 1, #players ) ] -- Our lucky player gets to be on team seek to start out!
		
		for k, ply in pairs( players ) do
		
			if ply && ply:IsValid() then
				
				if ply == luckyply then
					ply:SetTeam( TEAM_SEEK )
					ply:ChatPrint( "You are the starting Hider for this round!" )
				else
					ply:SetTeam( TEAM_HIDE )
				end
				
				ply:UnLock()
				ply:SendLua( "GAMEMODE.ShowScoreboard = false" ) -- TODO: Fix this.
				
				ply:KillSilent()				
				ply:Spawn()
				
			end
			
		end

	end
	
end

-- On initial spawn tell the client the where the hell everything is at.
function RoundsPlayerInitialSpawn( ply )
	SyncGameStatus( ply )
	SyncTimeLeft()
end

function SyncGameStatus( rf )
	if !rf then
		local rf = RecipientFilter()
		rf:AddAllPlayers()
	end
	
	umsg.Start( "SyncGameStatus", rf )
		umsg.Short( HNS_GAMESTATUS ) -- Sync the clients game status variable with ours.
	umsg.End()
end

function SyncTimeLeft()
	local rf = RecipientFilter()
	rf:AddAllPlayers()
	umsg.Start( "SyncTimeLeft", rf )
		umsg.Short( HNS_TIMELEFT ) -- Sync the clients time left variable.
	umsg.End()
end
timer.Create( "SyncTimeLeft", 30, 0, SyncTimeLeft )

